package com.homework.phase1.module4;

import java.util.Objects;

public class Student {
    private String id;
    private String name;
    private int age;

    public Student() {
    }

    public Student(String id, String name, int age) {
//        this.id = id;
//        this.name = name;
//        this.age = age;
        setId(id);
        setName(name);
        setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    //学号位数等于5时才可创建
    public void setId(String id) {
        if (id.length() == 5){
            this.id = id;
        } else {
            try {
                throw new IdException("学号输入有误，重新输入");
            } catch (IdException e) {
                e.printStackTrace();
            }
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age > 0 && age<150){
            this.age = age;
        } else {
            try {
                throw new AgeException("年龄输入有误，重新输入");
            } catch (AgeException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(id, student.id) &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age);
    }

    //对toString方法重新构建为以后方便从文件中进行数据提取，创建对象
    @Override
    public String toString() {
//        return "Student{" +
//                "id='" + id + '\'' +
//                ", name='" + name + '\'' +
//                ", age=" + age +
//                '}';
        return id + " " + name + " "+ age;
    }

    public String toString1() {
        return
                id + " " + name + " "+ age;
    }
}
